package com.pengchuang.ACS.admin.boot.controller;

import java.util.Arrays;
import java.util.Collections;
import java.util.Map;

import cn.hutool.core.util.StrUtil;
import com.pengchuang.ACS.admin.boot.entity.SysPermission;
import com.pengchuang.ACS.admin.boot.service.ISysPermissionService;
import com.pengchuang.ACS.common.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.pengchuang.ACS.admin.boot.entity.SysPermissionEntity;
import com.pengchuang.ACS.common.utils.PageUtils;
import com.pengchuang.ACS.common.utils.R;



/**
 * 权限表
 *
 * @author pengchaung
 * @email pengshi12138@outlook.com
 * @date 2023-03-14 15:33:20
 */
@RestController
@RequestMapping("/admin/permission")
public class SysPermissionController {
    @Autowired
    private ISysPermissionService sysPermissionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysPermissionService.queryPage(params);

        return R.ok().put("result", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        SysPermission sysPermission = sysPermissionService.getById(id);

        return R.ok().put("result", sysPermission);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SysPermission sysPermission){
		sysPermissionService.saveWithRole(sysPermission);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody SysPermission sysPermission){
		sysPermissionService.updateById(sysPermission);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestParam Long ids){
		sysPermissionService.removeByIds(Collections.singletonList(ids));
        return R.ok();
    }

    /**
     * 查找
     */
    @RequestMapping("/query")
    public R query(@RequestParam Map<String, Object> params){
        if (StrUtil.isBlank(String.valueOf(params.get("word")))) {
            return R.error(ResultCode.PARAM_VALID_FAIL);
        }
        PageUtils page = sysPermissionService.queryPageWithWord(params);
        return R.ok().put("page", page);
    }

}
